import React, { Component } from "react";
import { List, Toast } from "antd-mobile";
import MainLayout from "@/component/mainLayout/mainLayout";
import XHQBConfig from "@/config/XHQBConfig";
import yhTools from "@tools/yh";
import { agreements_url } from "@config/Config";
import "./myAgreement.scss";
const Item = List.Item;

class MyAgreement extends Component {
	constructor(props) {
		super(props);
		this.state = {
			list: [] //协议列表
		};
	}

	componentDidMount() {
		Toast.loading("加载中...", 100);
		let objs = {
			url: XHQBConfig.agreement_agreementList,
			params: {},
			_alertShow: false
		};
		yhTools.request(objs).then(res => {
			Toast.hide();
			if (res.code === "0") {
				this.setState({
					list: res.data
				});
			} else {
				Toast.info(res.message, 2);
			}
		});
	}

	nonStudentCommitmentLetter = type => {
		Toast.loading("加载中...", 100);
		let objs = {
			url: XHQBConfig.agreement_xhfPrePDF,
			params: {
				type
			},
			_alertShow: false
		};
		yhTools.request(objs).then(res => {
			Toast.hide();
			if (res.code === "0") {
				this.props.history.push({
					pathname: "/nonStudentCommitmentLetter",
					state: res.data
				});
			} else {
				Toast.info(res.message, 2);
			}
		});
	};

	/**
	 * @msg: 跳转路由
	 * @param {*}
	 * @return {*}
	 */
	toPath = (type, name) => {
		switch (type) {
			case "XHED":
				this.props.history.push({
					pathname: "/pdfAgreement",
					state: {
						type,
						name
					}
				});
				break;
			case "ZXSQ":
				this.props.history.push({
					pathname: "/pdfAgreement",
					state: {
						type,
						name
					}
				});
				break;
			case "XXSQ":
				this.props.history.push({
					pathname: "/pdfAgreement",
					state: {
						type,
						name
					}
				});
				break;
			case "XSCNH":
				this.nonStudentCommitmentLetter(type);
				break;
			case "YSXY":
				//cnd协议
				this.props.history.push({
					pathname: "/iframeAgreements",
					state: {
						src: agreements_url + "/privacyAgreement.html",
						title: name
					}
				});
				break;
			case "FWXY":
				this.props.history.push("/serviceAgreementAuthorization");
				break;
			default:
				break;
		}
	};

	render() {
		const { list } = this.state;
		console.log("list", list);
		const _pageParams = {
			header: {
				title: "设置"
			},
			history: this.props.history,
			cacheLifecycles: this.props.cacheLifecycles
		};
		return (
			<MainLayout pageParams={_pageParams}>
				<div className="myAgreement-page-content">
					<List className="my-list">
						{list &&
							list.length > 0 &&
							list.map(v => {
								return (
									<Item
										key={v.name}
										arrow="horizontal"
										onClick={() => {
											this.toPath(v.type, v.name);
										}}
									>
										{v.name}
									</Item>
								);
							})}
					</List>
				</div>
			</MainLayout>
		);
	}
}
export default MyAgreement;
